package com.xneure.function.mode;

import com.xneure.code.exception.FunctionException;
import com.xneure.function.enums.ParamType;

public class Param{
	protected String name;
	protected int index;
	protected String description;
	protected Object defaultValue;
	protected Object value;
	protected ParamType type;
	protected boolean isIgnore;
	
	public Param(String name,ParamType type,Object initValue,int index,boolean isIgnore){
		this.name = name;
		this.index = index;
		this.defaultValue = initValue;
		this.type = type;
		this.isIgnore = isIgnore;
	}

	public String getName(){
		return name;
	}

	public int getIndex(){
		return index;
	}

	public String getDescription(){
		return description;
	}

	public Object getDefaultValue(){
		return defaultValue;
	}

	public Object getValue(){
		return value;
	}

	public ParamType getType(){
		return type;
	}

	public void setDescription(String description){
		this.description = description;
	}

	public void setValue(Object value){
		if(value == null){
			if(isIgnore){
				throw new FunctionException("必填参数 " + name + " 不能为空。");
			}else{
				this.value = null;
			}
		}else{
			try{
				this.value = type.format(value);
			}catch(Exception e){
				throw new FunctionException("参数 " + name + " 解析错误。");
			}
		}
	}
}
